package string_question.leecode.test49;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/7 10:24
 */
public class Solution1 {

    // 方法一 ： 使用HashMap记录拍好序的key
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        if (strs == null || strs.length == 0) return res;
        HashMap<String, Integer> map = new HashMap<>();   // 存储拍好序的数组和对应res中的索引
        for (int i = 0; i < strs.length; i++) {
            String cur = strs[i];
            String key = sort(cur);
            if (map.containsKey(key)) {
                res.get(map.get(key)).add(cur);
            } else {
                map.put(key, res.size());
                res.add(new ArrayList<>());
                res.get(map.get(key)).add(cur);
            }
        }
        return res;
    }

    private String sort(String cur) {
        char[] chars = cur.toCharArray();
        Arrays.sort(chars);
        return String.valueOf(chars);
    }

    @Test
    public void test(){
        System.out.println(sort("sdbh"));
    }
}
